
// import * as DriverAction from '@/api/driver'
import * as CommonAction from '@/api/common'
import * as TaxiAction from '@/api/taxi'

// 移动端数据存储
export default {
  state: {
    mobilePath: '',
    mobileToken: {},
    mobileTarget: {},
    mobileDetail: {},
    mobileCarDetail: {},
    mobileUser: {},
  },
  getters: {
    mobilePath: state => state.mobilePath,
    mobileToken: state => state.mobileToken,
    mobileTarget: state => state.mobileTarget,
    mobileDetail: state => state.mobileDetail,
    mobileCarDetail: state => state.mobileCarDetail,
    mobileUserPhone: state => state.mobileUser.phone,
    isLogin: state => !!state.mobileToken.access_token
  },
  mutations: {
    setMobilePath(state, val) {
      state.mobilePath = val
    },
    clearMobilePath (state) {
      state.mobilePath = ''
    },
    setMobileToken (state, val) {
      state.mobileToken = val
    },
    setTarget (state, val) {
      state.mobileTarget = val
    },
    setDetail (state, val) {
      state.mobileDetail = val
    },
    clearDetail (state) {
      state.mobileDetail = {}
    },
    setMobileCar (state, val) {
      state.mobileCarDetail = val
    },
    setMobileUser (state, val) {
      state.mobileUser = val
    }
  },
  actions: {
    async getMobileDetail ({ commit, state }) {
      if (+state.mobileTarget.id === +state.mobileCarDetail.id && state.mobileDetail.id) return
      // const fun = state.mobileTarget.type === 'driver'
      //   ? TaxiAction.select
      //   : CompanyAction.select
      const res = await TaxiAction.select({ id: state.mobileTarget.id })
      if (res) {
        // if (res.type === 'taxi_company') res.type = 'company'
        const driver = res.driverList.find(item => item.status === 'on')
        commit('setMobileCar', res)
        commit('setDetail', driver)
      }
    },
    async getMobileUser ({ commit, state }) {
      const res = await CommonAction.userInfo()
      if (!res) return
      commit('setMobileUser', res.user)
    }
  }
}
